package com.suluyun.fragment;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.suluyun.R;
import com.suluyun.activity.LoginActivity;
import com.suluyun.activity.MainActivity;
import com.suluyun.presenter.LoginPresenter;
import com.suluyun.presenter.LoginPresenterCompl;
import com.suluyun.view.LoginView;

import zuo.biao.library.base.BaseFragment;

import static android.support.constraint.Constraints.TAG;

public class LoginFragment extends Fragment implements View.OnClickListener, LoginView {
    private View view;
    private Button buttonLogin;
    private Button buttonReg;
    private EditText editUser;
    private EditText editPass;
    private LoginPresenter loginPresenter;
    private ProgressBar progressBar;
    private RegFragment regFragment;
    public static LoginFragment createInstance(){
        return new LoginFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.login_fragment,container,false);
        //功能归类分区方法，必须调用<<<<<<<<<<
        initView();
        initData();
        initEvent();
        return view;
    }

    public void initView(){
        buttonLogin = (Button)view.findViewById(R.id.button_login);
        buttonReg = (Button)view.findViewById(R.id.button_reg);
        editUser = (EditText)view.findViewById(R.id.etUser);
        editPass = (EditText)view.findViewById(R.id.etPassWord);
        progressBar = (ProgressBar)view.findViewById(R.id.progress_login);

        loginPresenter = new LoginPresenterCompl(this);
        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);
    }

    public void initData(){

    }

    public void initEvent(){
        view.findViewById(R.id.button_login).setOnClickListener(this);
        view.findViewById(R.id.button_reg).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_login:
                loginPresenter.setProgressBarVisiblity(View.VISIBLE);
                buttonLogin.setEnabled(false);
                buttonReg.setEnabled(false);
                loginPresenter.doLogin(editUser.getText().toString(), editPass.getText().toString());
                Log.d("LoginActivity.java", "otherClick: " + editUser.getText().toString() + editPass.getText().toString());
                break;
            case R.id.button_reg:
                FragmentTransaction transaction = getFragmentManager().beginTransaction();
                hideAllFragment(transaction);
                Toast.makeText(getActivity(),"click",Toast.LENGTH_LONG).show();
                if (regFragment==null){
                    regFragment = RegFragment.createInstance();
                    transaction.add(R.id.login_fragment_container,regFragment);
                }else{
                    transaction.show(regFragment);
                }
                transaction.commit();
                break;
            default:
                break;
        }
    }

    @Override
    public void loginResult(Boolean result) {
        loginPresenter.setProgressBarVisiblity(View.INVISIBLE);
        buttonLogin.setEnabled(true);
        buttonReg.setEnabled(true);
        if (result){
            Activity temp = getActivity();
            startActivity(new Intent(getActivity(), MainActivity.class));
            temp.finish();
        }
        else{
            Toast.makeText(getActivity(),"账号或密码错误",Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSetProgressBarVisibility(int visibility) {
        progressBar.setVisibility(visibility);
        Log.d("LoginActivity.java", "onSetProgressBarVisibility: " + visibility);
    }

    public void hideAllFragment(FragmentTransaction transaction){
        if(regFragment!=null){
            transaction.hide(regFragment);
        }
    }
}
